
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.GroupLayout;
import javax.swing.JPanel;

import org.apache.commons.collections15.Transformer;

import edu.uci.ics.jung.algorithms.layout.FRLayout;
import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.control.ModalGraphMouse;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import edu.uci.ics.jung.visualization.renderers.Renderer.VertexLabel.Position;

public class Graph {
	VisualizationViewer<Node, Edge> Viserver;
	ArrayList<Node> nodes;
	JPanel panel;
	private FRLayout<Node, Edge> layout;
	private DirectedSparseMultigraph<Node, Edge> Graph;

	// Constructor
	Graph(ArrayList<Node> nodes, JPanel panel) {
		this.nodes = nodes;
		this.panel = panel;		
	}

	public VisualizationViewer GenGraph() {
		Graph = new DirectedSparseMultigraph<Node, Edge>();
		Iterator<Node> iter = nodes.iterator();

		while (iter.hasNext()) {
			Node node = iter.next();
			ArrayList<Node> neighbors = node.getNeighbors();
			Iterator<Node> it = neighbors.iterator();
			while (it.hasNext()) {
				Graph.addEdge(new Edge(), node, it.next());
			}
		}
		
		
		 Transformer<Node,Shape> vertexSize = new Transformer<Node,Shape>(){
	            public Shape transform(Node i){
	                Ellipse2D circle = new Ellipse2D.Double(-15, -15, 30, 30);
	                // in this case, the vertex is twice as large
	                return circle;
	            }
	        };

		// The Layout<V, E> is parameterized by the vertex and edge types
		layout = new FRLayout<Node, Edge>(Graph);
		int sx = panel.getWidth()-30;
		int sy = panel.getHeight()-30;
		Dimension s = new Dimension(sx,sy);
		layout.setSize(s);
		// layout.setSize(new Dimension(900,900)); // sets the initial size of
		// the layout space
		// The BasicVisualizationServer<V,E> is parameterized by the vertex and
		// edge types
		Viserver = new VisualizationViewer<Node, Edge>(layout);
		Viserver.setBackground(Color.DARK_GRAY);
		// Viserver.setPreferredSize(new Dimension(1000,1000)); //Sets the
		// viewing area size

		// Show vertex and edge labels
		Viserver.getRenderContext().setVertexShapeTransformer(vertexSize);
		Viserver.getRenderContext().setVertexLabelTransformer(new ToStringLabeller());
		Viserver.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
		// Viserver.getRenderContext().setEdgeLabelTransformer(new
		// ToStringLabeller());
		 // Create a graph mouse and add it to the visualization component
        DefaultModalGraphMouse gm = new DefaultModalGraphMouse();
        gm.setMode(ModalGraphMouse.Mode.PICKING);
        Viserver.setGraphMouse(gm); 

		GroupLayout Panelayout = new GroupLayout(panel);
		panel.setLayout(Panelayout);
		Panelayout.setAutoCreateGaps(true);
		Panelayout.setAutoCreateContainerGaps(true);
		Panelayout.setHorizontalGroup(Panelayout.createSequentialGroup()
				.addComponent(Viserver)
						
								
				);
		Panelayout.setVerticalGroup(Panelayout.createSequentialGroup()
				.addComponent(Viserver)
				);

		return Viserver;
	}

}
